from django.views import View

from Account.models import Account
from sportsground.utils import Req
from sportsground.utils.Res import Res


class ResetPwdView(View):
    """ 重置密码 """

    @Req.getJson
    def post(self, request):
        username = request.json_dict.get("username")
        role = request.json_dict.get("role")

        account = Account.objects.filter(username=username, role=role).first()
        if not account:
            return Res.Json(Res.Warn, msg="用户名不存在")

        account.set_password(username)
        account.save()
        return Res.Json(Res.OK, msg="已重置为用户名")
